<!DOCTYPE HTML>
<html>
<head>
    <title><%= __('views.myStore')%></title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>
    <style>
        .weui-row {
            margin: 8px 5px;
            display: -webkit-box;
            display: -ms-flexbox;
            display: -webkit-flex;
            display: flex;
            -webkit-box-pack: justify;
            -ms-flex-pack: justify;
            -webkit-justify-content: space-between;
            justify-content: space-between;
            -webkit-box-lines: multiple;
            -moz-box-lines: multiple;
            -webkit-flex-wrap: wrap;
            -ms-flex-wrap: wrap;
            flex-wrap: wrap;
            -webkit-box-align: start;
            -ms-flex-align: start;
            -webkit-align-items: flex-start;
            align-items: flex-start;
        }

        .weui_search_input {
            color: #2a2a2a !important;
        }
    </style>
</head>
<body>

<div class="weui_search_bar" id="search_bar">
    <form class="weui_search_outer">
        <div class="weui_search_inner">
            <i class="weui_icon_search"></i>
            <input type="search" class="weui_search_input" id="search_input" placeholder="<%= __('views.search')%>" required/>
            <a href="javascript:" class="weui_icon_clear" id="search_clear"></a>
        </div>
        <label for="search_input" class="weui_search_text" id="search_text">
            <i class="weui_icon_search"></i>
            <span><%= __('views.search')%></span>
        </label>
    </form>
    <a href="javascript:" class="weui_search_cancel" id="search_cancel"><%= __('views.return')%></a>
</div>
<div class="button_sp_area" style="margin: 0.25rem;">
    <a href="addDZ" class="weui_btn weui_btn_plain_default" style="font-size: 17px;line-height: 2;"><%= __('views.addStore')%></a>
</div>
<div class="weui_panel">
    <div class="weui_panel_bd dianzuk" id='list'>
    </div>
</div>
<input type="hidden" id="pageSize" value="10"/>
<input type="hidden" id="pageNumber" value="0"/>

<% include common/footer.ejs %>
<script>
    "use strict";
    $(function () {
        const customerId = '<%= userData.customerId %>';
        const accountType = '<%= userData.type %>';
        $('#search_input').focus(function(){
            $(this).closest('.weui_search_bar').addClass('weui_search_focusing');
        });

        $("#list").empty();
        $('#loadingDiv').html('<div class="infinite-preloader"></div>'+Dolphin.i18n.get("views.loading")+'...');
        $("#pageNumber").val("0");

        let data = {
            "category": "3",
            "pageSize": $("#pageSize").val(),
            "pageNumber": "0"
        };
        if(accountType == 2){
            data.parent_id_obj_ae = customerId;
        }else{
            data._areaId = customerId;
        }

        console.log(data);

        Dolphin.ajax({
            url: '/api/742445c653654caaa4a1fa69eb651a9a',
            type: Dolphin.requestMethod.POST,
            data: Dolphin.json2string({"properties": data}),
            onSuccess: function (reData) {
                $.each(reData.rows, function (i, n) {
                    $('<div class="weui_media_box weui_media_text show_detail" >' +
                        '<h4 class="weui_media_title">' + n.name + '</h4>' +
                        '<p class="weui_media_desc">'+ Dolphin.i18n.get("views.code") +':' + n.code + '</p>' +
                        '<p class="weui_media_desc">'+Dolphin.i18n.get("views.province")+':' + n.province +Dolphin.i18n.get("views.city") +':' + n.city + '</p>' +
                        '<p class="weui_media_desc">'+Dolphin.i18n.get("views.contactPerson")+':' + n.contactPerson + (n.mobile == "" ? n.telephone : n.mobile) + '</p>' +
                        '<p class="weui_media_desc">'+Dolphin.i18n.get("views.businessTime")+':' + n.openTime + ' - ' + n.closeTime + '</p>' +
                        '</div>').click(function () {
                        location.href = 'addDZ?id=' + n.id;

                    }).appendTo("#list");
                });
                $("#pageNumber").val(Number.parseInt($("#pageNumber").val()) + 1);
                $("#loadingDiv").empty();
            }
        });

        let loading = false;  //状态标记
        $(document.body).infinite().on("infinite", function () {
            if (loading) return;
            loading = true;
            setTimeout(function () {

                console.log($("#pageSize").val());
                console.log($("#pageNumber").val());
                let tempCondi = {
                    "category": "3",
                    "pageSize": $("#pageSize").val(),
                    "pageNumber": $("#pageNumber").val()
                }

                if(accountType == 2){
                    tempCondi.parent_id_obj_ae = customerId;
                }else{
                    tempCondi._areaId = customerId;
                }

                Dolphin.ajax({
                    url: '/api/742445c653654caaa4a1fa69eb651a9a',
                    type: Dolphin.requestMethod.POST,
                    data: Dolphin.json2string({"properties": tempCondi}),
                    onSuccess: function (reData) {

                        console.log(reData);
                        if (reData.rows == undefined || reData.rows.length == 0) {
                            $(document.body).destroyInfinite();
                            $("#loadingDiv").empty();
                        } else {
                            if ($('#loadingDiv').html() == '') {
                                $('#loadingDiv').html('<div class="infinite-preloader"></div>'+Dolphin.i18n.get("views.loading")+'...')
                            }
                            $.each(reData.rows, function (i, n) {
                                $('<div class="weui_media_box weui_media_text show_detail" >' +
                                    '<h4 class="weui_media_title">' + n.name + '</h4>' +
                                    '<p class="weui_media_desc">'+Dolphin.i18n.get("views.code")+':' + n.code + '</p>' +
                                    '<p class="weui_media_desc">'+Dolphin.i18n.get("views.province")+':' + n.province +Dolphin.i18n.get("views.province")+':' + n.city + '</p>' +
                                    '<p class="weui_media_desc">'+Dolphin.i18n.get("views.contactPerson")+':' + n.contactPerson + (n.mobile == "" ? n.telephone : n.mobile) + '</p>' +
                                    '<p class="weui_media_desc">'+Dolphin.i18n.get("views.businessTime")+':' + n.openTime + ' - ' + n.closeTime + '</p>' +
                                    '</div>').click(function () {
                                    location.href = 'addDZ?id=' + n.id;
                                }).appendTo("#list");
                            })
                            $("#pageNumber").val(Number.parseInt($("#pageNumber").val()) + 1);
                            loading = false;
                        }
                    }
                });
            }, 1500);   //模拟延迟
        });
        $("#search_input").blur(function () {
            let that = this;
            $(this).closest('.weui_search_bar').removeClass('weui_search_focusing');
            console.log('enter search input...');

            if ($('#loadingDiv').html() == '') {
                $('#loadingDiv').html('<div class="infinite-preloader"></div>'+Dolphin.i18n.get("views.loading")+'...')
            }

            let search = {
                "category": "3"
            };
            if(accountType == 2){
                search.parent_id_obj_ae = customerId;
            }else{
                search._areaId = customerId;
            }

            $("#pageNumber").val("0");

            if ($(that).val() == '') {
                search.pageSize = "10";
                search.pageNumber = "0";
            } else {
                search.name_like = $(that).val();     //<%= queryData.search %>
            }

            $("#list").empty();
            Dolphin.ajax({
                url: '/api/742445c653654caaa4a1fa69eb651a9a',
                type: Dolphin.requestMethod.POST,
                data: Dolphin.json2string({"properties": search}),
                onSuccess: function (reData) {
                    $.each(reData.rows, function (i, n) {

                        $('<div class="weui_media_box weui_media_text show_detail" >' +
                            '<h4 class="weui_media_title">' + n.name + '</h4>' +
                            '<p class="weui_media_desc">'+Dolphin.i18n.get("views.code")+':' + n.code + '</p>' +
                            '<p class="weui_media_desc">'+Dolphin.i18n.get("views.province")+':' + n.province +Dolphin.i18n.get("views.city")+ ':' + n.city + '</p>' +
                            '<p class="weui_media_desc">'+Dolphin.i18n.get("views.contactPerson")+':' + n.contactPerson + (n.mobile == "" ? n.telephone : n.mobile) + '</p>' +
                            '<p class="weui_media_desc">'+Dolphin.i18n.get("views.businessTime")+':' + n.openTime + ' - ' + n.closeTime + '</p>' +
                            '</div>').click(function () {
                            location.href = 'addDZ?id=' + n.id;
                        }).appendTo("#list");

                    });

                    $("#loadingDiv").empty();
                    if ($(that).val() != '') {
                        $(document.body).destroyInfinite();
                    } else {
                        $(document.body).infinite();
                        $("#pageNumber").val(Number.parseInt($("#pageNumber").val()) + 1);
                    }

                }
            });

        });
    });

</script>
<div class="weui-infinite-scroll" id="loadingDiv">
</div>
</body>
</html>